<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>千米与米</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="case12-demo3">
    千米:<input type="text" v-model="kilometers">
    米:<input type="text" v-model="meters">
    <p id="info"></p>
</div>
<script type="text/javascript">
    var case12Demo3=new Vue({
        el:"#case12-demo3",
        data:{
            kilometers:0,
            meters:0
        },
        watch:{
            kilometers: function (val) {
                this.kilometers=val;
                this.meters = this.kilometers*1000
            },
            meters:function (val) {
                this.kilometers=val/1000,
                    this.meters=val
            }
        }
    })
    //$watch是一个实例方法
    case12Demo3.$watch("kilometers",function (newValue,oldValue) {
        //这个回调将在case12Demo3.kilometers改变后调用
        document.getElementById("info").innerHTML="修改前值为: " + oldValue + "，修改后值为: " + newValue;
    })
</script>
</body>
</html>